import React from 'react'

type State = {
  x: number
  y: number
}

type Props = {
  render: (state: State) => React.ReactElement
}

export default class Mouse extends React.Component<Props> {
  state: State = {
    x: 0,
    y: 0,
  }

  move = (e: MouseEvent) => {
    this.setState({
      x: e.pageX,
      y: e.pageY,
    })
  }
  componentDidMount(): void {
    document.addEventListener('mousemove', this.move)
  }
  componentWillUnmount(): void {
    document.removeEventListener('mousemove', this.move)
  }

  render() {
    return (
      this.props.render(this.state)
    )
  }
}
